package com.eshop.modules.system.service.impl;

import java.io.IOException;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.io.Serializable;
import com.eshop.modules.system.domain.Dept;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.system.service.dto.JobDto;
import java.util.LinkedHashMap;
import java.util.List;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.system.service.dto.JobQueryCriteria;
import com.eshop.modules.system.service.DeptService;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.system.service.JobService;
import com.eshop.modules.system.domain.Job;
import com.eshop.modules.system.service.mapper.JobMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class JobServiceImpl extends BaseServiceImpl<JobMapper, Job> implements JobService
{
    private final IGenerator generator;
    private final DeptService deptService;
    
    public Map<String, Object> queryAll(final JobQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<Job> page = (PageInfo<Job>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)JobDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<Job> queryAll(final JobQueryCriteria criteria) {
        final List<Job> jobList = (List<Job>)((JobMapper)this.baseMapper).selectList(QueryHelpPlus.getPredicate(Job.class, criteria));
        if (criteria.getDeptIds().size() == 0) {
            for (final Job job : jobList) {
                job.setDept((Dept)this.deptService.getById((Serializable)job.getDeptId()));
            }
        }
        else {
            for (final Long deptId : criteria.getDeptIds()) {
                for (final Job job2 : jobList) {
                    if (deptId.equals(job2.getDeptId())) {
                        job2.setDept((Dept)this.deptService.getById((Serializable)job2.getDeptId()));
                    }
                }
            }
        }
        return jobList;
    }
    
    public void download(final List<JobDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final JobDto job : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("岗位名称", job.getName());
            map.put("岗位状态", job.getEnabled());
            map.put("岗位排序", job.getSort());
            map.put("创建日期", job.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public JobServiceImpl(final IGenerator generator, final DeptService deptService) {
        this.generator = generator;
        this.deptService = deptService;
    }
}
